# -*- conding:utf8 -*-
'''
Created on 2011-5-22

@author: Vendar
'''

from PyQt4 import QtGui,QtCore
from ui import PreferencesDlg_ui

class PreferencesDlg(QtGui.QDialog):
    Signal_ReqLogin = QtCore.pyqtSignal('QWidget')
    def __init__(self,parent=None,wflags=0):
        super(QtGui.QDialog,self).__init__(parent)
        
        #init ui
        self.ui = PreferencesDlg_ui.Ui_PreferencesDlg()
        self.ui.setupUi(self)
        
        #setting variables
        self.UserName = ""
        self.Password = ""
        self.AccountHistory = []

        #init ui
        self.CheckStatus()
        
        #setup connections
        self.ui.loginButton.clicked.connect(self.OnLogin)
        self.ui.OKButton.clicked.connect(self.OnOK)
        self.ui.CancelButton.clicked.connect(self.OnCancel)
        
    def SaveSettings(self):
        pass
    
    def CheckStatus(self):
        idx = self.GetAccountHistoryIndex(self.UserName)
        if idx <0 and len(self.UserName)>0:
            self.AccountHistory.append(self.UserName)
            self.ui.accountCombo.clear()
            self.ui.accountCombo.insertItems(0,self.AccountHistory)
        else:
            self.ui.accountCombo.setCurrentIndex(idx)
 
    def GetAccountHistoryIndex(self,account):
        for i in range(0,len(self.AccountHistory)-1):
            if self.AccountHistory[i] == self.UserName:
                return i
        return -1        
 
    def GetUserName(self):
        return self.UserName
    
    def GetPassword(self):
        return self.Password
    
    def Show(self):
        idx = self.GetAccountHistoryIndex(self.UserName)
        self.ui.accountCombo.setCurrentIndex(idx)
        return self.exec_()
 
    @QtCore.pyqtSlot(str,str)
    def OnAccountChanged(self,username,password):
        self.UserName = username
        self.Password = password
        self.CheckStatus()
 
    @QtCore.pyqtSlot()
    def OnLogin(self):
        self.Signal_ReqLogin.emit(self)
    
    @QtCore.pyqtSlot()
    def OnOK(self):
        self.accept()
        self.SaveSettings()
        
    @QtCore.pyqtSlot()
    def OnCancel(self):
        self.reject()
        